package com.pospi.pospihold.util;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.pospihold.App;
import com.pospi.pospihold.dto.GoodsDto;
import com.pospi.pospihold.dto.ModifiedDto;
import com.pospi.pospihold.dto.ModifiedGroupDto;
import com.pospi.pospihold.dto.PayBeen;
import com.pospi.pospihold.dto.ReturnDto;
import com.pospi.pospihold.greendao.PayBeenDao;
import com.pospi.pospihold.http.LoadServer;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by acer on 2018/9/27.
 */

public class DownUtil {
    private Context mContext;

    public DownUtil(Context mContext) {
        this.mContext = mContext;
    }

    public void downYs() {
        RequestParams params = new RequestParams();
        params.put("value", mContext.getSharedPreferences("StoreMessage", MODE_PRIVATE).getString("Id", ""));
        LoadServer.getConnect(mContext, Api.SyncModified, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.d("util", new String(bytes));
                try {
                    JSONObject object = new JSONObject(new String(bytes));
                    ReturnDto loginReturnDto = new ReturnDto();
                    loginReturnDto.setResult(object.getInt("Result"));
                    loginReturnDto.setMessage(object.getString("Message"));
                    if (loginReturnDto.getResult() == 1) {
                        SPHelper.getInstance().putString(Api.YS, object.getString("Value"));
                    }


                    Gson gson = new Gson();
                    List<ModifiedDto> dtos = gson.fromJson(object.getString("Value"), new TypeToken<List<ModifiedDto>>() {
                    }.getType());
                    Log.i("dto.size", dtos.size() + "样式");



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            }
        });
    }
    public void downYsz() {
        RequestParams params = new RequestParams();
        params.put("value", mContext.getSharedPreferences("StoreMessage", MODE_PRIVATE).getString("Id", ""));
        LoadServer.getConnect(mContext, Api.SyncModifiedGroup, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.d("util", new String(bytes));
                try {
                    JSONObject object = new JSONObject(new String(bytes));
                    ReturnDto loginReturnDto = new ReturnDto();
                    loginReturnDto.setResult(object.getInt("Result"));
                    loginReturnDto.setMessage(object.getString("Message"));
                    if (loginReturnDto.getResult() == 1) {
                        SPHelper.getInstance().putString(Api.YSZ, object.getString("Value"));
                    }

                    Gson gson = new Gson();
                    List<ModifiedGroupDto> dtos = gson.fromJson(object.getString("Value"), new TypeToken<List<ModifiedGroupDto>>() {
                    }.getType());
                    Log.i("dto.size", dtos.size() + "样式组");



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
    public void downPay(){
        RequestParams params = new RequestParams();
        LoadServer.getConnect(mContext, Api.GET_PAY, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.d("util", new String(bytes));
                try {
                    JSONObject object = new JSONObject(new String(bytes));
                    JSONArray array = object.getJSONArray("Value");
                    List<PayBeen> beens = new Gson().fromJson(array.toString(), new TypeToken<List<PayBeen>>() {
                    }.getType());
                    PayBeenDao dao = App.getInstance().getDaoSession().getPayBeenDao();
                    dao.deleteAll();
                    dao.insertInTx(beens);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.d("pay", new String(bytes));
            }
        });
    }
}
